package com.myt.matp.utils;
   
import javax.microedition.lcdui.Canvas;  
   
public final class KeyCodeAdapter {  
   
    /** 
     * constants for keycodes 
     */
    // this key is present on Nokia s60 
    public static final int KEY_1 = 201;  
    public static final int KEY_2 = 202;  
    public static final int KEY_3 = 203;  
    public static final int KEY_4 = 204;  
    public static final int KEY_5 = 205;  
    public static final int KEY_6 = 206;  
    public static final int KEY_7 = 207;  
    public static final int KEY_8 = 208;  
    public static final int KEY_9 = 209;  
    public static final int KEY_0 = 200;  
    public static final int KEY_POUND = 211;  
    public static final int KEY_STAR = 212;  
    public static final int KEY_PENCIL = -207;  
    public static final int KEY_DELETE = -204;  
    public static final int KEY_BACK = -205;  
    public static final int KEY_SEND = -206;
    //Keys on joistch
    public static final int KEY_UP = 221;  
    public static final int KEY_DOWN = 222;  
    public static final int KEY_LEFT = 223;  
    public static final int KEY_RIGHT = 224;  
    public static final int KEY_CENTER = 225;  
    // Soft keys
    public static final int KEY_SOFT_LEFT = -201;  
    public static final int KEY_SOFT_RIGHT = -202;  
    public static final int KEY_SOFT_MIDDLE_INTERNET = -203;  
    // Undefined
    public static final int KEY_NOT_DEFINED = 254;  
   

    /** 
     * current platform codeofSoftkey 
     */  
    private final int PLATFORM_KEY_SOFT_LEFT;
    private final int PLATFORM_KEY_SOFT_RIGHT;
    private final int PLATFORM_KEY_SOFT_MIDLE_INTERNET;
    private final int PLATFORM_KEY_SOFT_DELETE;
    private final int PLATFORM_KEY_SOFT_BACK;
   
    /** 
     * standart values for softkeys of different platforms 
     * used only in predefining 
     */
    // SonyEricsson
    private static final int SE_KEY_SOFT_LEFT = -6;  
    private static final int SE_KEY_SOFT_RIGHT = -7;  
    private static final int SE_KEY_DELETE = -8;  
    private static final int SE_KEY_INTERNET = -10;  
    private static final int SE_KEY_BACK = -11;
    // Samsung
    private static final int SAMSUNG_KEY_SOFT_LEFT = -6;  
    private static final int SAMSUNG_KEY_SOFT_RIGHT = -7;  
    private static final int SAMSUNG_KEY_DELETE = -8;  
    // Siemens
    private static final int SIEMENS_KEY_SOFT_LEFT = -1;  
    private static final int SIEMENS_KEY_SOFT_RIGHT = -4;  
    // Nokia
    private static final int NOKIA_KEY_SOFT_MIDLE = -5;  
    private static final int NOKIA_KEY_SOFT_LEFT = -6;  
    private static final int NOKIA_KEY_SOFT_RIGHT = -7;  
    private static final int NOKIA_KEY_DELETE = -8;  
    private static final int NOKIA_KEY_PENCIL = -50;  
    // Motorola
    private static final int MOTOROLA_KEY_SOFT_LEFT = -21;  
    private static final int MOTOROLA_KEY_SOFT_LEFT1 = 21;
    private static final int MOTOROLA_KEY_SOFT_LEFT2 = -20;  
    private static final int MOTOROLA_KEY_SOFT_RIGHT = -22;    
    private static final int MOTOROLA_KEY_SOFT_RIGHT1 = 22;  
    private static final int MOTOROLA_KEY_SOFT_RIGHT2 =-21;  
    private static final int MOTOROLA_KEY_SOFT_MIDLE = -23;  

   
    private static final String SOFT_WORD = "SOFT";  

    private Canvas m_Canvas = null;
   
    /** 
     * constructor. 
     * here is predefining of spesial keys
     */  
    public KeyCodeAdapter(Canvas c) {  
        //#ifdef DEBUG_KEYCODEADAPTER
        //# System.out.println(getClass().getName() + ".KeyCodeAdapter(Canvas c) {");
        //#endif
	m_Canvas = c;
	int platform = Platform.id(c);
	PLATFORM_KEY_SOFT_LEFT = getLeftSoftkeyCode(platform);  
	PLATFORM_KEY_SOFT_RIGHT = getRightSoftkeyCode(platform);  
	PLATFORM_KEY_SOFT_MIDLE_INTERNET = getMidleORInternetSoftkeyCode(platform);  
	PLATFORM_KEY_SOFT_DELETE = getDeleteKeyCode(platform);  
	PLATFORM_KEY_SOFT_BACK = getBackKeyCode(platform);

        //#ifdef DEBUG_KEYCODEADAPTER
        //# System.out.println("\tPLATFORM_KEY_SOFT_LEFT = " + PLATFORM_KEY_SOFT_LEFT);
        //# System.out.println("\tPLATFORM_KEY_SOFT_RIGHT = " + PLATFORM_KEY_SOFT_RIGHT);
        //# System.out.println("\tPLATFORM_KEY_SOFT_MIDLE_INTERNET = " + PLATFORM_KEY_SOFT_MIDLE_INTERNET);
        //# System.out.println("\tPLATFORM_KEY_SOFT_DELETE = " + PLATFORM_KEY_SOFT_DELETE);
        //# System.out.println("\tPLATFORM_KEY_SOFT_BACK = " + PLATFORM_KEY_SOFT_BACK);
        //#endif

        //#ifdef DEBUG_GUI
        //# System.out.println("}");
        //#endif
    }  

    private boolean detectKeyCodeByName(int keyCode, String name) {
	boolean ret = false;
	String keyName = "";  
	try {  
	    keyName = m_Canvas.getKeyName(keyCode).toUpperCase();
	    ret = (keyName.indexOf(name.toUpperCase()) >= 0); 
	} catch (IllegalArgumentException ilae) {  
	    // ilae.printStackTrace();  
	    ret = false;
	}
	return ret;
    }

    private boolean detectSoftKeyCodeByName(int keyCode, String name) {
	boolean ret = false;
	String keyName = "";  
	try {  
	    keyName = m_Canvas.getKeyName(keyCode).toUpperCase();
	    ret = (keyName.indexOf(SOFT_WORD) >= 0 && keyName.indexOf(name) >= 0); 
	} catch (IllegalArgumentException ilae) {  
	    // ilae.printStackTrace();  
	    ret = false;
	}
	return ret;
    }
     
    /**
     * define real left soft key code by platform 
     * 
     * @param platform an <code>int</code> value
     * @return code
     */
    private int getLeftSoftkeyCode(int platform) {
	int keyCode = NOKIA_KEY_SOFT_LEFT;  

	switch (platform) {
	case Platform.MOTOROLA_ID:
	    if (detectSoftKeyCodeByName(MOTOROLA_KEY_SOFT_LEFT, "1") ||
		detectSoftKeyCodeByName(MOTOROLA_KEY_SOFT_LEFT, "LEFT")) {
		keyCode = MOTOROLA_KEY_SOFT_LEFT;
	    } else if (detectSoftKeyCodeByName(MOTOROLA_KEY_SOFT_LEFT1, "1") ||
		       detectSoftKeyCodeByName(MOTOROLA_KEY_SOFT_LEFT1, "LEFT")) {
		keyCode = MOTOROLA_KEY_SOFT_LEFT1;
	    } else if (detectSoftKeyCodeByName(MOTOROLA_KEY_SOFT_LEFT2, "1") ||
		       detectSoftKeyCodeByName(MOTOROLA_KEY_SOFT_LEFT2, "LEFT")) {
		keyCode = MOTOROLA_KEY_SOFT_LEFT2;
	    }
	    break;
	case Platform.NOKIA_ID:
	    keyCode = NOKIA_KEY_SOFT_LEFT;
	    break;
	case Platform.SAMSUNG_ID:
	    keyCode = SAMSUNG_KEY_SOFT_LEFT;
	    break;
	case Platform.SIEMENS_ID:
	    if (detectSoftKeyCodeByName(SIEMENS_KEY_SOFT_LEFT, "1") ||
		detectSoftKeyCodeByName(SIEMENS_KEY_SOFT_LEFT, "LEFT")) {
		keyCode = SIEMENS_KEY_SOFT_LEFT;
	    }
	    break;
	case Platform.SONYERICSSON_ID:
	    keyCode = SE_KEY_SOFT_LEFT;
	    break;
	default:
	    for (int i = -125; i <= 125; i++) {  
		if (i == 0) {  
		    i++;  
		}
		if (detectSoftKeyCodeByName(i, "1") ||
		    detectSoftKeyCodeByName(i, "LEFT")) {
		    keyCode = i;
		    break;
		}
	    }  
	    break;
	}

	return keyCode;  
    }  

    /**
     * define real right soft key code by platform 
     * 
     * @param platform an <code>int</code> value
     * @return code
     */
    private int getRightSoftkeyCode(int platform) {
	int keyCode = NOKIA_KEY_SOFT_RIGHT;  

	switch (platform) {
	case Platform.MOTOROLA_ID:
	    if (detectSoftKeyCodeByName(MOTOROLA_KEY_SOFT_RIGHT, "2") ||
		detectSoftKeyCodeByName(MOTOROLA_KEY_SOFT_RIGHT, "RIGHT")) {
		keyCode = MOTOROLA_KEY_SOFT_RIGHT;
	    } else if (detectSoftKeyCodeByName(MOTOROLA_KEY_SOFT_RIGHT1, "2") ||
		       detectSoftKeyCodeByName(MOTOROLA_KEY_SOFT_RIGHT1, "RIGHT")) {
		keyCode = MOTOROLA_KEY_SOFT_RIGHT1;
	    } else if (detectSoftKeyCodeByName(MOTOROLA_KEY_SOFT_RIGHT2, "2") ||
		       detectSoftKeyCodeByName(MOTOROLA_KEY_SOFT_RIGHT2, "RIGHT")) {
		keyCode = MOTOROLA_KEY_SOFT_RIGHT2;
	    }
	    break;
	case Platform.NOKIA_ID:
	    keyCode = NOKIA_KEY_SOFT_RIGHT;
	    break;
	case Platform.SAMSUNG_ID:
	    keyCode = SAMSUNG_KEY_SOFT_RIGHT;
	    break;
	case Platform.SIEMENS_ID:
	    if (detectSoftKeyCodeByName(SIEMENS_KEY_SOFT_RIGHT, "4") ||
		detectSoftKeyCodeByName(SIEMENS_KEY_SOFT_RIGHT, "RIGHT")) {
		keyCode = SIEMENS_KEY_SOFT_RIGHT;
	    }
	    break;
	case Platform.SONYERICSSON_ID:
	    keyCode = SE_KEY_SOFT_RIGHT;
	    break;
	default:
	    for (int i = -125; i <= 125; i++) {  
		if (i == 0) {  
		    i++;  
		}
		if (detectSoftKeyCodeByName(i, "2") ||
		    detectSoftKeyCodeByName(i, "4") ||
		    detectSoftKeyCodeByName(i, "RIGHT")) {
		    keyCode = i;
		    break;
		}
	    }  
	    break;
	}

	return keyCode;  
    }  
   
    /**
     * define real middle soft key code for current platform 
     * 
     * @param platform an <code>int</code> value
     * @return code
     */
    private int getMidleORInternetSoftkeyCode(int platform) {  
	int keyCode = 0;
	switch (platform) {
	case Platform.MOTOROLA_ID:
	    if (detectSoftKeyCodeByName(MOTOROLA_KEY_SOFT_MIDLE, "SOFT") ||
		detectKeyCodeByName(MOTOROLA_KEY_SOFT_MIDLE, "OK")) {
		keyCode = MOTOROLA_KEY_SOFT_MIDLE;
	    }
	    break;
	case Platform.NOKIA_ID:
	    if (detectSoftKeyCodeByName(NOKIA_KEY_SOFT_MIDLE, "SOFT")) {
		keyCode = NOKIA_KEY_SOFT_MIDLE;
	    }
	    break;
	case Platform.SAMSUNG_ID:
	case Platform.SIEMENS_ID:
	case Platform.SONYERICSSON_ID:
	    keyCode = SE_KEY_INTERNET;
	    break;
	}

	return keyCode;
    }  
   
    /**
     * define real key's C or DELETE code for current platform 
     * 
     * @param platform an <code>int</code> value
     * @return code
     */
    private int getDeleteKeyCode(int platform) {  
	int keyCode = 0;

	switch (platform) {
	case Platform.NOKIA_ID:
	    keyCode = NOKIA_KEY_DELETE;
	    break;
	case Platform.SAMSUNG_ID:
	    if (detectSoftKeyCodeByName(SAMSUNG_KEY_DELETE, "CLEAR")) {
		keyCode = SAMSUNG_KEY_DELETE;
	    }
	    break;
	case Platform.SIEMENS_ID:
	case Platform.SONYERICSSON_ID:
	    keyCode = SE_KEY_DELETE;
	    break;
	}

	return keyCode;
    }  
   
   
    /**
     * define real key's BACK code for current platform 
     * 
     * @param platform an <code>int</code> value
     * @return code
     */
    private int getBackKeyCode(int platform) {  
	int keyCode = 0;
	switch (platform) {
	case Platform.SONYERICSSON_ID:
	    keyCode = SE_KEY_BACK;
	    break;
	default:
	    break;
	}
	return keyCode;
    }  

    /**
     * Describe <code>adoptKeyCode</code> method here.
     *
     * @param keyCode an <code>int</code> value
     * @return an <code>int</code> value
     */
    public int adoptKeyCode(int keyCode) {

	switch (keyCode) {
	case Canvas.KEY_NUM0:  
	    keyCode = KEY_0;  
	    break;
	case Canvas.KEY_NUM1:  
	    keyCode = KEY_1;  
	    break;
	case Canvas.KEY_NUM2:  
	    keyCode = KEY_2;  
	    break;
	case Canvas.KEY_NUM3:  
	    keyCode = KEY_3;  
	    break;
	case Canvas.KEY_NUM4:  
	    keyCode = KEY_4;  
	    break;
	case Canvas.KEY_NUM5:  
	    keyCode = KEY_5;  
	    break;
	case Canvas.KEY_NUM6:  
	    keyCode = KEY_6;  
	    break;
	case Canvas.KEY_NUM7:  
	    keyCode = KEY_7;  
	    break;
	case Canvas.KEY_NUM8:  
	    keyCode = KEY_8;  
	    break;
	case Canvas.KEY_NUM9:  
	    keyCode = KEY_9;  
	    break;
	case Canvas.KEY_STAR:  
	    keyCode = KEY_STAR;  
	    break;
	case Canvas.KEY_POUND:  
	    keyCode = KEY_POUND;  
	    break;
	default:
	    // Soft keys
	    if (keyCode == PLATFORM_KEY_SOFT_LEFT) {
		keyCode = KEY_SOFT_LEFT;
	    } else if (keyCode == PLATFORM_KEY_SOFT_RIGHT) {
		keyCode = KEY_SOFT_RIGHT;
	    } else if (keyCode == PLATFORM_KEY_SOFT_DELETE) {
		keyCode = KEY_DELETE;
	    } else if (keyCode == PLATFORM_KEY_SOFT_MIDLE_INTERNET) {
		keyCode = KEY_SOFT_MIDDLE_INTERNET;
	    } else if (keyCode == PLATFORM_KEY_SOFT_BACK) {
		keyCode = KEY_BACK;
	    } else if (keyCode == NOKIA_KEY_PENCIL) {
		keyCode = KEY_PENCIL;
	    } else {
		keyCode = adoptGameKeyCode(keyCode);
	    }  
	}

	return keyCode;  
    }

    private int adoptGameKeyCode(int keyCode) {
	try {  
	    final int gameAction;  
	    gameAction = m_Canvas.getGameAction(keyCode);  
	    switch (gameAction) {
	    case Canvas.UP:
		keyCode = KEY_UP;
		break;
	    case Canvas.DOWN:
		keyCode = KEY_DOWN;
		break;
	    case Canvas.LEFT:
		keyCode = KEY_LEFT;
		break;
	    case Canvas.RIGHT:
		keyCode = KEY_RIGHT;
		break;
	    case Canvas.FIRE:
		keyCode = KEY_CENTER;
		break;
	    default:
		break;
	    }
	} catch (IllegalArgumentException e) {  
	    // e.printStackTrace();  
	}
	return keyCode;
    }
} 